package com.sunjob.mroresource.service.impl;

import com.github.pagehelper.PageHelper;
import com.sunjob.common.domain.resource.Staff;
import com.sunjob.common.domain.resource.StaffPosition;
import com.sunjob.common.domain.resource.StaffTitle;
import com.sunjob.common.domain.resource.StaffType;
import com.sunjob.common.util.MROUtils;
import com.sunjob.mroresource.mapper.StaffMapper;
import com.sunjob.mroresource.service.StaffService;
import com.sunjob.mroresource.util.ResourceUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author: TomHuang
 * @date: 2022/8/11 9:38
 */
@Service
public class StaffServiceImpl implements StaffService {

//    @Autowired(required = false)
    @Resource
    private StaffMapper staffMapper;


    @Override
    public Staff selectStaffById(Long staffId) {
        return staffMapper.queryStaffById(staffId);
    }

    @Override
    public Staff selectStaffByStaffJobId(String staffJobId) {
        //校验工号格式
        ResourceUtils.checkJobId(staffJobId);
        return staffMapper.queryStaffByStaffJobId(staffJobId);
    }

    @Override
    public List<Staff> selectStaffByStaffName(String staffName) {
        return staffMapper.queryStaffByStaffName(staffName);
    }

    @Override
    public Staff selectStaffBy(Long StaffId, String StaffJobId, String StaffName) {
        return staffMapper.queryStaffBy(StaffId, StaffJobId, StaffName);
    }

    @Override
    public List<Staff> selectAllStaff() {
        return staffMapper.queryAllStaff();
    }

    @Override
    public int selectStaffCount() {
        return staffMapper.queryStaffCount();
    }

    @Override
    public int insertStaff(Staff staff) {
        //使用自动生成Id
        staff.setStaffId(MROUtils.nextId());

        //新增员工时间即为任职时间
        staff.setStaffTenure(LocalDateTime.now());

        //校验工号格式
        ResourceUtils.checkJobId(staff.getStaffJobId());

        //校验手机号格式
        ResourceUtils.checkPhone(staff.getStaffPhone());

        //校验身份证
        ResourceUtils.checkIc(staff.getStaffIc());

        //校验员工类型id范围
        int typeBiggestId=staffMapper.queryBiggestTypeId();
        ResourceUtils.checkRange(typeBiggestId,staff.getStaffTypeId());

        //校验员工职位id范围
        int positionBiggestId=staffMapper.queryBiggestPositionId();
        ResourceUtils.checkRange(positionBiggestId,staff.getStaffPositionId());

        //校验员工技术职称id范围
        int titleBiggestId=staffMapper.queryBiggestTitleId();
        ResourceUtils.checkRange(titleBiggestId,staff.getStaffTitleId());

        return staffMapper.insertStaff(staff);
    }

    @Override
    public List<String> selectAllStaffType() {
        return staffMapper.queryAllStaffType();
    }


    @Override
    public int insertStaffType(StaffType staffType) {
        return staffMapper.insertStaffType(staffType);
    }

    @Override
    public List<String> selectAllStaffPosition() {
        return staffMapper.queryAllStaffPosition();
    }


    @Override
    public int insertStaffPosition(StaffPosition staffPosition) {
        return staffMapper.insertStaffPosition(staffPosition);
    }

    @Override
    public List<String> selectAllStaffTitle() {
        return staffMapper.queryAllStaffTitle();
    }


    @Override
    public int insertStaffTitle(StaffTitle staffTitle) {
        return staffMapper.insertStaffTitle(staffTitle);
    }

    @Override
    public int updateStaff(Staff staff) {
        //校验工号格式
        ResourceUtils.checkJobId(staff.getStaffJobId());
        //校验手机号格式
        ResourceUtils.checkPhone(staff.getStaffPhone());
        //校验身份证
        ResourceUtils.checkIc(staff.getStaffIc());
        //校验员工类型id范围
        int typeBiggestId=staffMapper.queryBiggestTypeId();
        ResourceUtils.checkRange(typeBiggestId,staff.getStaffTypeId());

        //校验员工职位id范围
        int positionBiggestId=staffMapper.queryBiggestPositionId();
        ResourceUtils.checkRange(positionBiggestId,staff.getStaffPositionId());

        //校验员工技术职称id范围
        int titleBiggestId=staffMapper.queryBiggestTitleId();
        ResourceUtils.checkRange(titleBiggestId,staff.getStaffTitleId());
        return staffMapper.updateStaff(staff);
    }

    @Override
    public int updateStaffStatus(Long staffId,int staffStatus) {
        return staffMapper.updateStaffStatus(staffId,staffStatus);
    }

    @Override
    public String selectTypeByStaffId(Long staffId) {
        return staffMapper.queryTypeByStaffId(staffId);
    }

    @Override
    public String selectPositionByStaffId(Long staffId) {
        return staffMapper.queryPositionByStaffId(staffId);
    }

    @Override
    public String selectTitleByStaffId(Long staffId) {
        return staffMapper.queryTitleByStaffId(staffId);
    }

    @Override
    public int selectBiggestTypeId() {
        return staffMapper.queryBiggestTypeId();
    }

    @Override
    public int selectBiggestPositionId() {
        return staffMapper.queryBiggestPositionId();
    }

    @Override
    public int selectBiggestTitleId() {
        return staffMapper.queryBiggestTitleId();
    }


}
